<?php

namespace apiend\modules\book\controllers;

use Yii;
use apiend\components\Controller;
use apiend\modules\book\models\BookViewSearch;
use apiend\modules\book\models\BookForm;
use apiend\modules\book\models\BookStaffForm;
use common\models\book\Stage;
use apiend\modules\book\models\ToggleShelfForm;

/**
 * 商品管理
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ApplyController extends Controller {

    /**
     * @inheritdoc
     */
    public $modelClass = BookViewSearch::class;

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            '*' => ['POST'],
            'update' => ['GET', 'POST'],
            'check' => ['GET'],
            'params' => ['GET', 'POST'],
        ];
    }

    /**
     * 商品信息编辑
     * 
     * @param integer|null $id 商品ID，null时为添加新的商品
     * @return mixed
     */
    public function actionUpdate($id = null) {
        $model = new BookForm($id);
        if (Yii::$app->request->isPost) {
            $params = Yii::$app->request->post();
            if (!$model->load($params, '')) {
                parent::error('数据提交失败');
            }
            return $model->save();
        }
        return $model;
    }

    /**
     * @inheritdoc
     */
    public function actionCheck() {
        $date = Yii::$app->request->get('date');
        $bookId = Yii::$app->request->get('book_id');
        return Stage::findAllEffective($date, $bookId);
    }

    /**
     * 商品参数编辑
     * 
     * @param integer|null $id 商品ID，null时为添加新的商品
     * @return mixed
     */
    public function actionParams($id) {
        $model = new BookStaffForm($id);
        if (Yii::$app->request->isPost) {
            $params = Yii::$app->request->post();
            if (!$model->load($params, '')) {
                parent::error('数据提交失败');
            }
            return $model->save();
        }
        return $model;
    }

    /**
     * 下架商品
     * @inheritdoc
     */
    public function actionOffshelf($id) {
        $model = new ToggleShelfForm();
        $model->items = [$id];
        $model->type = ToggleShelfForm::SHELF_OFF;
        return $model->save();
    }

    /**
     * 提交上架商品审核
     * @inheritdoc
     */
    public function actionOnshelf($id) {
        $model = new ToggleShelfForm();
        $model->items = [$id];
        $model->type = ToggleShelfForm::SHELF_ON;
        return $model->save();
    }

    /**
     * 提交上架商品审核
     * @inheritdoc
     */
    public function actionToggleShelf() {
        $model = new ToggleShelfForm();
        if (Yii::$app->request->isPost) {
            $params = Yii::$app->request->post();
            if (!$model->load($params, '')) {
                parent::error('数据提交失败');
            }
            return $model->save();
        }
        return $model;
    }

    /**
     * 删除商品（店铺）
     * @inheritdoc
     */
    public function actionRemove($id) {
        $model = $this->findModel($id);
        if (!$model->canRemove) {
            parent::warning('当前商品不能删除');
        }
        return $model->delete();
    }

    /**
     * 删除商品（管理员）
     * @inheritdoc
     */
    public function actionDelete($id) {
        $model = $this->findModel($id);
        if (!$model->canDelete) {
            parent::warning('当前商品不能删除');
        }
        return $model->delete();
    }

    /**
     * 提交审核商品（管理员）
     * @inheritdoc
     */
    public function actionAudit() {
        $model = new AuditForm();
        $model->admin_id = $this->uid;
        if (Yii::$app->request->isPost) {
            $params = Yii::$app->request->post();
            if (!$model->load($params, '')) {
                parent::error('数据提交失败');
            }
            return $model->save();
        }
        return $model;
    }

}
